﻿// This code is released under the BSD license.
namespace PugLib.UnitTest
{
    #region

    using System.Collections.Generic;
    using System.ComponentModel;
    using Filters;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using TestCollateral;
    using UI.Controls;

    #endregion

    /// <summary>
    ///This is a test class for SearchFilterQueryTest and is intended
    ///to contain all SearchFilterQueryTest Unit Tests
    ///</summary>
    [TestClass]
    public class SearchFilterQueryTest
    {
        [TestMethod]
        public void ExecuteTest()
        {
            string[] FieldsForDemo = new[]
                                         {
                                             "Language", "Number", "Domain", "Name", "Operating System", "Boot Date"
                                             ,
                                             "IP Address"
                                         };
            BindingList<Computer> DataForDemo = new BindingList<Computer>();

            SearchFilterGroup _searchGroup = new SearchFilterGroup();
            _searchGroup.SetFields(FieldsForDemo);
            Assert.IsNotNull(_searchGroup.Fields);

            _searchGroup.MatchType = MatchStyle.Any;
            _searchGroup.SetFilters(new List<SearchFilter>
                                        {
                                            new SearchFilter
                                                {
                                                    Field = "Domain",
                                                    SearchOperator = SearchOperator.Contains,
                                                    SearchText = "x"
                                                },
                                            new SearchFilter
                                                {
                                                    Field = "Name",
                                                    SearchOperator = SearchOperator.DoesNotContain,
                                                    SearchText = "x"
                                                },
                                            //new SearchFilter
                                            //    {
                                            //        Field = "DOES NOT EXIST",
                                            //        SearchOperator = SearchOperator.IsNot,
                                            //        SearchText = null
                                            //    },
                                            new SearchFilter
                                                {
                                                    Field = "Boot Date",
                                                    SearchOperator = SearchOperator.IsBefore,
                                                    SearchText = "2008"
                                                },
                                            new SearchFilter
                                                {
                                                    Field = "Language",
                                                    SearchOperator = SearchOperator.IsNot,
                                                    SearchText = "de"
                                                },
                                        });

            List<Computer> computers = new List<Computer>();

            computers.Add(new Computer
                              {
                                  Domain = null,
                                  Name = null,
                                  OperatingSystem = null,
                                  BootDate = null,
                                  IPAddress = null,
                                  Number = null,
                                  Language = null
                              });


            Repeat.Call<Computer>(computers.Add, Computer.CreateRandom, 50);


            computers.ForEach(DataForDemo.Add);

            IList<Computer> filteredItems = SearchFilterQuery.Execute(DataForDemo, _searchGroup);
            Assert.IsNotNull(filteredItems);

            _searchGroup.MatchType = MatchStyle.All;
            filteredItems = SearchFilterQuery.Execute(DataForDemo, _searchGroup);
            Assert.IsNotNull(filteredItems);


            using (FilterBuilder builder = new FilterBuilder())
            {
                builder.RemoveDuplicatesOnGetFilters = true;

                builder.SetState(_searchGroup);
                builder.TestShow();
                Assert.IsNotNull(builder.Filters);
            }
        }
    }
}